package com.xuecheng.user.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.api.client.CheckcodeClient;
import com.xuecheng.user.enums.AuthType;
import com.xuecheng.user.exception.AuthCellPhoneHasNoRegisterException;
import com.xuecheng.user.exception.AuthCheckCodeNotCorrectException;
import com.xuecheng.user.mapper.XcUserMapper;
import com.xuecheng.user.model.dto.AuthParamsDTO;
import com.xuecheng.user.model.dto.XcUserExt;
import com.xuecheng.user.model.po.XcUser;
import com.xuecheng.user.service.AuthService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * @author 勾新杰
 * @version 1.0
 * @description: 手机短信认证
 * @date 2025/3/10 15:33
 */
@Service("smsAuthService")
@RequiredArgsConstructor
public class SmsAuthServiceImpl implements AuthService {

    private final CheckcodeClient checkcodeClient;

    private final XcUserMapper xcUserMapper;

    /**
     * 认证方法
     *
     * @param authParamsDTO 认证参数
     * @return XcUserExt
     */
    @Override
    public XcUserExt auth(AuthParamsDTO authParamsDTO) {
        Boolean verify = checkcodeClient.verify(authParamsDTO.getCheckcodekey(), authParamsDTO.getCheckcode());
        if (!verify) AuthCheckCodeNotCorrectException.cast();
        XcUser xcUser = xcUserMapper.selectOne(new LambdaQueryWrapper<XcUser>()
                .eq(XcUser::getCellPhone, authParamsDTO.getCellphone()));
        if (xcUser == null) AuthCellPhoneHasNoRegisterException.cast();
        return BeanUtil.copyProperties(xcUser, XcUserExt.class);
    }

    /**
     * 获取认证类型
     *
     * @return AuthType
     */
    @Override
    public AuthType getAuthType() {
        return AuthType.SMS;
    }
}
